﻿namespace BlackHole.DAO
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;

    public static partial class Utility
    {
        public static List<Product> GetPagedProductsDetailByOwnerId(int districtId, int ownerId = 0, int deviceId = 0, int statusId = 0, int maximumRows = 50, int startRowIndex = 0)
        {
            List<Product> ret = new List<Product>();
            using (SqlConnection conn = new SqlConnection(ConnStr))
            {
                SqlCommand cmd = new SqlCommand("GetPagedProductsDetailByOwnerId", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@districtId", districtId);
                cmd.Parameters.AddWithValue("@ownerId", ownerId);
                cmd.Parameters.AddWithValue("@deviceId", deviceId);
                cmd.Parameters.AddWithValue("@statusId", statusId);
                cmd.Parameters.AddWithValue("@maximumRows", maximumRows);
                cmd.Parameters.AddWithValue("@startRowIndex", startRowIndex);
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Product product = new Product();
                    product.Id = (int)reader["Id"];
                    product.ProductDeviceId = (int)reader["DeviceId"];
                    product.ProductDistrictId = (int)reader["DistrictId"];
                    product.ProductOwnerId = (int)reader["OwnerId"];
                    product.ProductDeviceName = reader["DeviceName"].ToString();
                    product.ProductDistrictName = reader["DistrictName"].ToString();
                    product.ProductOwnerName = reader["OwnerName"].ToString();
                    product.StatusId = (int)reader["StatusId"];
                    product.StatusName = Enum.GetName(typeof(ProductStatus), product.StatusId);
                    product.SerialNO = reader["SerialNO"].ToString();
                    product.PsamNO = reader["PsamNO"].ToString();
                    product.RomVersion = reader["ROMVersion"].ToString();
                    int tmpint = 0;
                    int.TryParse(reader["RepairCycle"].ToString(), out tmpint);
                    product.RepairCycle = tmpint;
                    ret.Add(product);
                }
            }

            return ret;
        }

        public static int GetProductsCountByOwnerId(int districtId, int ownerId, int deviceId = 0, int statusId = 0)
        {
            int ret;
            using (SqlConnection conn = new SqlConnection(ConnStr))
            {
                SqlCommand cmd = new SqlCommand("GetProductsCountByOwnerId", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@districtId", districtId);
                cmd.Parameters.AddWithValue("@ownerId", ownerId);
                cmd.Parameters.AddWithValue("@deviceId", deviceId);
                cmd.Parameters.AddWithValue("@statusId", statusId);
                conn.Open();
                ret = (int)cmd.ExecuteScalar();
            }

            return ret;
        }
    }
}